import { Injectable } from '@nestjs/common';
import { PrismaService } from '@admin/common/service/prisma/prisma.service';
import { Response } from 'express';
import { exportTable } from '@admin/common/utils';
import { QueryJpBannerDto, CreateJpBannerDto, UpdateJpBannerDto } from '../dto/index';
import { Prisma } from '@prismaClient';
import { isNotEmpty, isEmpty } from 'class-validator';
@Injectable()
export class JpBannerService {
  constructor(private prisma: PrismaService) {}
  /**@description 查询家谱Banner所有 */
  async selectBannerAll() {
    return this.prisma.jpBanner.findMany();
  }
  /**@description 分页查询家谱Banner列表 */
  async selectBannerList(q: QueryJpBannerDto) {
    const queryCondition: Prisma.JpBannerWhereInput = {};
    if (isNotEmpty(q['image'])) {
      queryCondition.image = {
        equals: q.image
      };
    }
    if (isNotEmpty(q['describe'])) {
      queryCondition.describe = {
        equals: q.describe
      };
    }
    if (isNotEmpty(q['status'])) {
      queryCondition.status = {
        equals: q.status
      };
    }
    if (isNotEmpty(q['sort'])) {
      queryCondition.sort = {
        equals: q.sort
      };
    }
    return {
      rows: await this.prisma.jpBanner.findMany({
        skip: (q.pageNum - 1) * q.pageSize,
        take: q.pageSize,
        where: queryCondition
      }),
      total: await this.prisma.jpBanner.count({
        where: queryCondition
      })
    };
  }
  /**@description 查询家谱Banner详情 */
  async selectBannerById(id: number) {
    return this.prisma.jpBanner.findUnique({
      where: {
        id
      }
    });
  }
  /**@description 新增家谱Banner */
  async addBanner(jpBanner: CreateJpBannerDto) {
    if (isEmpty(jpBanner['image'])) {
      delete jpBanner['image'];
    }
    if (isEmpty(jpBanner['describe'])) {
      delete jpBanner['describe'];
    }
    if (isEmpty(jpBanner['status'])) {
      delete jpBanner['status'];
    }
    if (isEmpty(jpBanner['sort'])) {
      delete jpBanner['sort'];
    }
    if (isEmpty(jpBanner['createTime'])) {
      delete jpBanner['createTime'];
    }
    if (isEmpty(jpBanner['updateTime'])) {
      delete jpBanner['updateTime'];
    }
    return await this.prisma.jpBanner.create({
      data: jpBanner
    });
  }
  /**@description 修改家谱Banner */
  async updateBanner(jpBanner: UpdateJpBannerDto) {
    if (isEmpty(jpBanner['image'])) {
      delete jpBanner['image'];
    }
    if (isEmpty(jpBanner['describe'])) {
      delete jpBanner['describe'];
    }
    if (isEmpty(jpBanner['status'])) {
      delete jpBanner['status'];
    }
    if (isEmpty(jpBanner['sort'])) {
      delete jpBanner['sort'];
    }
    if (isEmpty(jpBanner['createTime'])) {
      delete jpBanner['createTime'];
    }
    if (isEmpty(jpBanner['updateTime'])) {
      delete jpBanner['updateTime'];
    }
    return await this.prisma.jpBanner.update({
      where: {
        id: jpBanner.id
      },
      data: jpBanner
    });
  }
  /**@description 批量删除家谱Banner */
  async deleteBannerByIds(ids: number[]) {
    return this.prisma.jpBanner.deleteMany({
      where: {
        id: {
          in: ids
        }
      }
    });
  }
  /**@description 单个删除家谱Banner */
  async deleteBannerById(id: number) {
    return this.prisma.jpBanner.delete({
      where: {
        id
      }
    });
  }
  /**@description 导出家谱Banner所有数据为xlsx */
  async exportBanner(res: Response) {
    const title = ['ID', '图片', '描述', '状态(0禁用，1正常)', '排序', '注册时间', '修改时间'];
    const data = (await this.prisma.jpBanner.findMany()).map(v => Object.values(v));
    data.unshift(title);
    exportTable(data, res);
  }
}
